//
//  LoginViewController.m
//  huashiedu
//
//  Created by oceans on 2018/7/28.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "MoblieLoginViewController.h"
#import "Utility.h"

@interface LoginViewController ()

@property (strong, nonatomic) UITextField *username;
@property (strong, nonatomic) UITextField *password;
@property (strong, nonatomic) UIButton *secureButton;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubViews];
}

- (void)setupSubViews {
    
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"登录";
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.view addGestureRecognizer:tap];
    
    UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
    close.frame = CGRectMake(0, 0, 44, 44);
    [close setImage:[UIImage imageNamed:@"chacha"] forState:UIControlStateNormal];
    [close addTarget:self action:@selector(toClose) forControlEvents:UIControlEventTouchUpInside];
    UIView *closeView = [[UIView alloc] initWithFrame:close.bounds];
    [closeView addSubview:close];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:closeView];
    
    UIImageView *imagev = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
    imagev.center = CGPointMake(ScreenWidth * 0.5, 87 + XNavigationHeight);
    imagev.image = [UIImage imageNamed:@"loginicon"];
    [self.view addSubview:imagev];
    
    self.username = [[UITextField alloc] initWithFrame:CGRectMake(31, CGRectGetMaxY(imagev.frame) + 46, ScreenWidth - 62, 35)];
    _username.keyboardType = UIKeyboardTypeNumberPad;
    _username.placeholder = @"请输入手机号";
    [self.view addSubview:_username];
    
    CALayer *topline = [[CALayer alloc] init];
    topline.frame = CGRectMake(30, CGRectGetMaxY(_username.frame) + 6.5, ScreenWidth - 60, 0.5);
    topline.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:topline];
    
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(31, CGRectGetMaxY(topline.frame) + 17, ScreenWidth - 62, 35)];
    _password.placeholder = @"密码";
    _password.secureTextEntry = YES;
    [self.view addSubview:_password];
    
    CALayer *botline = [[CALayer alloc] init];
    botline.frame = CGRectMake(30, CGRectGetMaxY(_password.frame) + 6.5, ScreenWidth - 60, 0.5);
    botline.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:botline];
    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    login.frame = CGRectMake(31, CGRectGetMaxY(botline.frame) + 35.0, ScreenWidth - 62, 50);
    login.backgroundColor = COLOR(253, 119, 77, 1);
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    login.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [login setTitle:@"登录" forState:UIControlStateNormal];
    [login addTarget:self action:@selector(toLogin) forControlEvents:UIControlEventTouchUpInside];
    login.layer.cornerRadius = 3.0;
    login.layer.masksToBounds = YES;
    [self.view addSubview:login];
    
    self.secureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _secureButton.frame = CGRectMake(0, 0, 35, 35);
    [_secureButton addTarget:self action:@selector(showOrHidePassword:) forControlEvents:UIControlEventTouchUpInside];
    [_secureButton setImage:[UIImage imageNamed:@"pass_secu"] forState:UIControlStateNormal];
    UIView *view = [[UIView alloc] initWithFrame:_secureButton.bounds];
    [view addSubview:_secureButton];
    _password.rightView = view;
    _password.rightViewMode = UITextFieldViewModeAlways;
    
    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    left.frame = CGRectMake(31, CGRectGetMaxY(login.frame) + 17, (ScreenWidth - 62) / 3.0, 30);
    [left setTitleColor:COLOR(115, 117, 116, 1) forState:UIControlStateNormal];
    left.titleLabel.font = [UIFont systemFontOfSize:14.0];
    left.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [left setTitle:@"手机快捷登录" forState:UIControlStateNormal];
    [left addTarget:self action:@selector(toMobileLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:left];
    
    UIButton *center = [UIButton buttonWithType:UIButtonTypeCustom];
    center.frame = CGRectMake(31 + (ScreenWidth - 62) / 3.0, CGRectGetMaxY(login.frame) + 17, (ScreenWidth - 62) / 3.0, 30);
    [center setTitleColor:COLOR(115, 117, 116, 1) forState:UIControlStateNormal];
    center.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [center setTitle:@"新用户注册" forState:UIControlStateNormal];
    [center addTarget:self action:@selector(toRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:center];
    
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    right.frame = CGRectMake(31 + (ScreenWidth - 62) / 3.0 * 2.0, CGRectGetMaxY(login.frame) + 17, (ScreenWidth - 62) / 3.0, 30);
    [right setTitleColor:COLOR(115, 117, 116, 1) forState:UIControlStateNormal];
    right.titleLabel.font = [UIFont systemFontOfSize:14.0];
    right.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [right setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [right addTarget:self action:@selector(toForgetPassword) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:right];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 136.0, 20)];
    title.center = CGPointMake(ScreenWidth * 0.5, ScreenHeight - XNavigationHeight - 100);
    title.textAlignment = NSTextAlignmentCenter;
    title.font = [UIFont systemFontOfSize:14.0];
    title.textColor = COLOR(165, 165, 165, 1);
    title.text = @"其他方式登录";
    [self.view addSubview:title];
    
    CALayer *leftLine = [[CALayer alloc] init];
    leftLine.frame = CGRectMake(10, ScreenHeight - XNavigationHeight - 100, (ScreenWidth - 136 - 20) * 0.5, 0.5);
    leftLine.backgroundColor = [UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:leftLine];
    
    CALayer *rightLine = [[CALayer alloc] init];
    rightLine.frame = CGRectMake((ScreenWidth + 136) * 0.5, ScreenHeight - XNavigationHeight - 100, (ScreenWidth - 136 - 20) * 0.5, 0.5);
    rightLine.backgroundColor = [UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:rightLine];
    
    UIButton *wechat = [UIButton buttonWithType:UIButtonTypeCustom];
    wechat.frame = CGRectMake(0, 0, 47, 47);
    wechat.center = CGPointMake(ScreenWidth * 0.5 - 50, ScreenHeight - XNavigationHeight - 50);
    [wechat setImage:[UIImage imageNamed:@"wechatlogin"] forState:UIControlStateNormal];
    [wechat addTarget:self action:@selector(loginWithWechat) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:wechat];
    wechat.hidden = YES;
    
    if ([WXApi isWXAppInstalled]) {
        wechat.hidden = NO;
    }
    
    UIButton *qq = [UIButton buttonWithType:UIButtonTypeCustom];
    qq.frame = CGRectMake(0, 0, 47, 47);
    qq.center = CGPointMake(ScreenWidth * 0.5 + 50, ScreenHeight - XNavigationHeight - 50);
    [qq setImage:[UIImage imageNamed:@"qqlogin"] forState:UIControlStateNormal];
    [qq addTarget:self action:@selector(loginWithQQ) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:qq];
    qq.hidden = YES;
    if ([TencentOAuth iphoneQQInstalled] || [TencentOAuth iphoneTIMInstalled]) {
        qq.hidden = NO;
    }
}

- (void)showOrHidePassword:(UIButton*)sender {
    
    if (_password.secureTextEntry) {
        _password.secureTextEntry = NO;
        [_secureButton setImage:[UIImage imageNamed:@"pass_secu_no"] forState:UIControlStateNormal];
    }else {
        _password.secureTextEntry = YES;
        [_secureButton setImage:[UIImage imageNamed:@"pass_secu"] forState:UIControlStateNormal];
    }
}

- (void)toClose {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)toLogin {
    
    if ([Utility checkMobile:_username.text] && _password.text.length > 0) {
        [self.view endEditing:YES];
        __weak typeof(self) weakself = self;
        [[UserModel shared] loginWithPassword:self.password.text username:self.username.text successBlock:^{
            [weakself toClose];
        }];
    }
}

- (void)toMobileLogin {
    
    MoblieLoginViewController *controller = [[MoblieLoginViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)toRegister {
    
    RegisterViewController *regi = [[RegisterViewController alloc] init];
    regi.isRegister = YES;
    [self.navigationController pushViewController:regi animated:YES];
}

- (void)toForgetPassword {
    
    RegisterViewController *regi = [[RegisterViewController alloc] init];
    regi.isRegister = NO;
    [self.navigationController pushViewController:regi animated:YES];
}

- (void)loginWithWechat {
    __weak typeof(self) weakself = self;
    [[UserModel shared] loginWithThirdParty:TPL_WECHAT successBlock:^{
        [weakself toClose];
    } failedBlock:^{
        
    }];
}

- (void)loginWithQQ {
    __weak typeof(self) weakself = self;
    [[UserModel shared] loginWithThirdParty:TPL_QQ successBlock:^{
        [weakself toClose];
    } failedBlock:^{
        
    }];
}

- (void)hideKeyboard {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

+ (UINavigationController *)loginViewController {
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:[[LoginViewController alloc] init]];
    controller.view.backgroundColor = [UIColor whiteColor];
    controller.navigationBar.tintColor = [UIColor blackColor];
    [controller.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:63/255.0 green:63/255.0 blue:63/255.0 alpha:1.0]}];
    return controller;
}


@end
